package com.winderinfo.yikaotianxia.ui.banxing;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaselanFragment;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.ui.classification.FenLieApapter;
import com.winderinfo.yikaotianxia.ui.classification.ListBean;
import com.winderinfo.yikaotianxia.ui.xiangqin.DetailsActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class BanXinListFragment extends BaselanFragment implements Contract.View {

    @BindView(R.id.b_recycle)
    RecyclerView bRecycle;
    Unbinder unbinder;
    private Presenter presenter;
    Map<String, Object> map = new HashMap<>();

    @Override
    protected int layoutResID() {
        return R.layout.activity_banxin;
    }

    @Override
    protected void initView(View view) {
        //实例P
        presenter = new Presenter(this);
        //适配管理器
        bRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    protected void loadData() {
        //专业ID
        int position = getArguments().getInt("position");
        //请求分类
        /**
         *
         *@pageNum 页码
         *@pageSize 每页数量
         *@type 分类1班型2课程3教材
         * @p_id 专业id
         *
         * */
        map.clear();
        map.put("pageNum", 1);
        map.put("pageSize", 10);
        map.put("type", 1);
        map.put("pId", position);
        presenter.onPostRequest(Api.LIEBIAO, map, ListBean.class);

    }

    @Override
    public void onSuccess(Object data) {
        //商品列表
        if (data instanceof ListBean) {
            List<ListBean.RowsBean> rows = ((ListBean) data).getRows();
            FenLieApapter fenLieApapter = new FenLieApapter(getActivity(), rows);
            bRecycle.setAdapter(fenLieApapter);
            //接口回调
            fenLieApapter.setOnTypeFeel(new FenLieApapter.OnTypeFeel() {
                @Override
                public void getId(int id, String type, String feel) {
                    //判断TYPE，1跳到班型详情，其他详情
                    //Feel , 0跳到详情，1跳到学习
                    if (type.equals("2")) {
                        if (feel.equals("0")) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("id", id);
                            bundle.putString("type", type);
                            openActivity(DetailsActivity.class, bundle);
                        } else {
                            //跳到学习页
                        }
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putInt("id", id);
                        bundle.putString("type", type);
                        openActivity(DetailsActivity.class, bundle);
                    }
                }
            });
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }
}
